package com.xd.core.debt.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xd.core.debt.entity.DebtTransRelate;
import com.xd.core.debt.mapper.DebtTransRelateMapper;
import com.xd.core.debt.service.IDebtTransRelateService;
import com.xd.core.finance.vo.DebtOrderQueryVo;
import com.xd.core.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZX
 * @since 2025-03-05
 */
@Service
public class DebtTransRelateServiceImpl extends ServiceImpl<DebtTransRelateMapper, DebtTransRelate> implements IDebtTransRelateService {

    @Autowired
    private DebtTransRelateMapper debtTransRelateMapper;

    @Autowired
    private ISysUserService iSysUserService;

    @Override
    public List<DebtOrderQueryVo> getDebtOrderRelate(String orderNo) {
        List<DebtOrderQueryVo> resultList = new ArrayList<>();
        List<DebtOrderQueryVo> result = debtTransRelateMapper.getDebtOrderRelate(orderNo);
        //将结果按照姓名分组再将结果按照债权价值升序排列
        Map<String, List<DebtOrderQueryVo>> map = result.stream().collect(Collectors.groupingBy(DebtOrderQueryVo::getName));
        map.forEach((name, list) -> {
            list.sort(Comparator.comparing(DebtOrderQueryVo::getDebtValue));
        });
        resultList = map.values().stream().flatMap(List::stream).collect(Collectors.toList());
        return resultList;
    }
}
